package hengtiansoft.com.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.hengtiansoft.pulltorefresh.PullToRefreshBase;
import com.hengtiansoft.pulltorefresh.PullToRefreshListView;

import java.util.ArrayList;

import hengtiansoft.com.myapplication.game.SwipeMenuListView;
import hengtiansoft.com.myapplication.game.TestGameActivity;
import hengtiansoft.com.myapplication.map.MapActivity;
import hengtiansoft.com.myapplication.view.EditextView;
import hengtiansoft.com.myapplication.view.FirstVolley;
import hengtiansoft.com.myapplication.view.SecondVolley;

/**
 * Created by lu on 16/1/26.
 */
public class MyListView extends Activity {
    PullToRefreshListView view;
    MyAdaper myadaper;
    ArrayList<String> my = new ArrayList<>();
    Intent intent;
    int t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylistview);
        intent = new Intent();
        int i = 0;
        while (i < 20) {
            i++;
            my.add("" + i);
        }
        initView();
    }

    private void initView() {
        view = (PullToRefreshListView) findViewById(R.id.view);
        myadaper = new MyAdaper(my);
        view.getListView().setAdapter(myadaper);
        view.setPullLoadEnabled(true);
        view.setScrollLoadEnabled(false);
        view.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                my.clear();
                int i = 0;
                t++;
                while (i < 5) {
                    i++;
                    my.add("" + i);
                }
                if (t > 1) {
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
                myadaper = new MyAdaper(my);
                view.getListView().setAdapter(myadaper);
                view.onPullDownRefreshComplete();
                view.onPullUpRefreshComplete();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                int i = 0;
                while (i < 5) {
                    i++;
                    my.add("" + i);
                }
                myadaper = new MyAdaper(my);
                int y = my.size();
                view.getListView().setAdapter(myadaper);
                view.onPullDownRefreshComplete();
                view.onPullUpRefreshComplete();
                view.getListView().setSelectionFromTop(y, TRIM_MEMORY_BACKGROUND);
            }
        });

        view.getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Log.e("lu", String.valueOf(position));
                switch (position) {
                    case 0:
                        intent.setClass(MyListView.this, FirstVolley.class);
                        break;
                    case 1:
                        intent.setClass(MyListView.this, SecondVolley.class);
                        break;
                    case 2:
                        intent.setClass(MyListView.this, EditextView.class);
                        break;
                    case 3:
                        intent.setClass(MyListView.this, MapActivity.class);
                        break;
                    case 4:
                        intent.setClass(MyListView.this, TestGameActivity.class);
                        break;
                    case 5:
                        intent.setClass(MyListView.this, SwipeMenuListView.class);
                        break;
                }
                if (null == intent) {
                    return;
                }
                startActivity(intent);
            }
        });
    }


    public class MyAdaper extends BaseAdapter {
        private ArrayList<String> m1;

        public MyAdaper(ArrayList<String> s) {
            m1 = s;
        }

        @Override
        public int getCount() {
            return m1.size();
        }

        @Override
        public Object getItem(int i) {
            return m1.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ViewHolder holder;
            if (view == null) {
                view = View.inflate(MyListView.this, R.layout.myitem, null);
                holder = new ViewHolder();
                holder.textView = (TextView) view.findViewById(R.id.textView4);
                view.setTag(holder);
            } else {
                holder = (ViewHolder) view.getTag();
            }
            String s = "";
            //这里只能设置单个holder中
            switch (i) {
                case 0:
                    s = "layoutInflater";
                    break;
                case 1:
                    s = "isMyLove";
                    break;
                case 2:
                    s = "editText";
                    break;
                case 3:
                    s = MapActivity.class.getSimpleName();
                    break;
                case 4:
                    s = "TestGame";
                    break;
                case 5:
                    s = SwipeMenuListView.class.getSimpleName();
                    break;

            }
            /**
             * 新版本androidStudio有一个快速编译,运行的功能
             * 写好代码之后编译运行发现新写的代码没有执行,
             * 启动DeBug,新写的断点都断不下来.询问同事之后得知可在Build选项中Clean Project.之后可以正常运行新写的代码.
             * 快速运行的功能在写界面改动的时候作用明显,可以立即看到
             */
            holder.textView.setText(s);
//            holder.textView.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View view) {
//                    startActivity(intent);
//                }
//            });
            return view;
        }

        class ViewHolder {
            TextView textView;
        }
    }

}
//mPullListView=(PullToRefreshListView)findViewById(
//        R.id.lv_collection);
//        // 上拉，下拉
//        mPullListView.setPullLoadEnabled(true);
//        mPullListView.setScrollLoadEnabled(false);
//        mLvCollection=mPullListView.getRefreshableView();
//        mLvCollection.setDivider(null);